package cn.edu.hqu.cst.zwl.aidldemo;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;

import java.util.Timer;
import java.util.TimerTask;

public class AidlService extends Service {
    private CatBinder catBinder;
    private Timer timer=new Timer();
    private String[] colors=new String[]{"红色","黄色","黑色"};
    private double[] weights=new double[]{2.3,3.1,1.58};
    private String color;
    private double weight;

    public AidlService() {
    }

    @Override
    public IBinder onBind(Intent intent) {
        return catBinder;
    }
    class CatBinder extends ICat.Stub{

        @Override
        public void basicTypes(int anInt, long aLong, boolean aBoolean, float aFloat, double aDouble, String aString) throws RemoteException {

        }

        @Override
        public String getColor() throws RemoteException {
            return AidlService.this.color;
        }

        @Override
        public double getWeight() throws RemoteException {
            return AidlService.this.weight;
        }
    }

    @Override
    public void onCreate() {
        super.onCreate();
        catBinder=new CatBinder();
         timer.schedule(new TimerTask() {
             @Override
             public void run() {
                 int rand=(int) (Math.random()*3);
                 color=colors[rand];
                 weight=weights[rand];
             }
         },0,800);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        timer.cancel();
    }
}